package org.freetek.linkdn;

import org.freetek.linkdn.activity.login.LoginActivity;
import org.freetek.linkdn.activity.login.LoginBundleKeys;
import org.freetek.linkdn.util.communication.LinkedInAuthWrapper;
import org.freetek.linkdn.util.communication.LinkedInConstants;
import org.freetek.linkdn.util.model.LinkedInAuthResponse;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class Linkdn extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// TODO should be made by a factory
		final LinkedInAuthWrapper authenticator = new LinkedInAuthWrapper();
		final LinkedInAuthResponse authResponce = authenticator.auth();

		startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(LinkedInConstants.LINKEDIN_LOGIN_URL
				+ authResponce.getRequestToken())));

		// TODO if there is a key stored in the DB already, skip the login
		// screen
		startActivityForResult(getLoginIntent(authResponce), RESULT_FIRST_USER);
	}

	private Intent getLoginIntent(final LinkedInAuthResponse authResponce) {
		final Intent loginIntent = new Intent(Linkdn.this, LoginActivity.class);

		Bundle extras = new Bundle();
		extras.putSerializable(LoginBundleKeys.AUTH_RESPONSE, authResponce);
		loginIntent.putExtras(extras);
		return loginIntent;
	}

}